package com.b511.swpu.cementbackend.service.impl;

import com.b511.swpu.cementbackend.dto.input.BoreholeConditionInputDto;
import com.b511.swpu.cementbackend.entity.Borehole;
import com.b511.swpu.cementbackend.entity.Stratum;
import com.b511.swpu.cementbackend.repository.StratumRepository;
import com.b511.swpu.cementbackend.service.StratumService;
import com.b511.swpu.cementbackend.util.Help;
import com.b511.swpu.cementbackend.util.R;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.JoinType;

@Service
public class StratumServiceImpl implements StratumService {

    private final StratumRepository stratumRepository;

    public StratumServiceImpl(StratumRepository stratumRepository){
        this.stratumRepository = stratumRepository;
    }

    @Override
    public R getStratumByCondition(BoreholeConditionInputDto boreholeConditionInputDto, PageRequest pageRequest){
        return R.ok().putData(stratumRepository.findAll((Specification<Stratum>) (root, cq, cb)
                -> Help.createPredicate(boreholeConditionInputDto, root.join("borehole", JoinType.LEFT), cb), pageRequest));
    }
}
